﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices; //for calling user32.dll
using System.Threading; //for Thread.Sleep
//using System.Windows.Input;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();

            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            this.BackColor = Color.Transparent;

            //System.Windows.Input.Mouse.Capture();

            //this.Capture = true;

            //TODO: delete can form get all system mouse coords? yes
            //while (true)
            //{
            //    Console.WriteLine(System.Windows.Forms.Cursor.Position);
            //}

        }

        ////Ref: http://msdn.microsoft.com/en-us/library/ms646290.aspx
        //[return: MarshalAs(UnmanagedType.Bool)]
        //[DllImport("user32.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
        //public static extern bool BlockInput([In, MarshalAs(UnmanagedType.Bool)] bool fBlockIt);

        /// Return Type: BOOL->int
        ///fBlockIt: BOOL->int
        [System.Runtime.InteropServices.DllImportAttribute("user32.dll", EntryPoint = "BlockInput")]
        [return: System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)]
        public static extern bool BlockInput([System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.Bool)] bool fBlockIt);



        //Ref: http://stackoverflow.com/questions/586547/how-can-i-block-keyboard-and-mouse-input-in-c
        public static void BlockInput(TimeSpan span)
        {
            try
            {
                BlockInput(true);
                Thread.Sleep(span);
            }
            finally
            {
                BlockInput(false);
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //int xPos = 0;
            //int yPos = 0;

            //try
            //{
            //    xPos = Int32.Parse(this.textBox1.Text);
            //}
            //catch(Exception ex)
            //{
            //    Console.WriteLine(this.textBox1.Text + " is not a valid number");
            //}

            //try
            //{
            //    yPos = Int32.Parse(this.textBox2.Text);

            //}
            //catch (Exception ex)
            //{
            //    Console.WriteLine(this.textBox2.Text + " is not a valid number");
            //}
 
            //this.Location = new Point(xPos, yPos);

        }

        private void showForm()
        {
            this.hideStatus.Text = "";
        }

        private void hideForm()
        {
            //check if cursor is within bounds of form
            Point cursorPos = Cursor.Position;
            Point cursorPosClient = this.PointToClient(cursorPos);
            
            //Point formStartPt = this.Location;
            //Point formEndPt = new Point(formStartPt.X + this.Width, formStartPt.Y + this.Height);
            Point formStartPt = this.PointToScreen(this.panel1.Location);
            Point formEndPt = this.PointToScreen(
                new Point(this.panel1.Location.X + this.panel1.Width, this.panel1.Location.Y + this.panel1.Height) );


            Console.WriteLine("cursorPos: " + cursorPos);
            this.txtCursorPos.Text = cursorPos.ToString();
            this.txtCursorPosClient.Text = cursorPosClient.ToString();

            Console.WriteLine("client rect: " + this.ClientRectangle);
            this.txtClientRect.Text = this.ClientRectangle.ToString();
            this.txtFormStartPt.Text = formStartPt.ToString();
            this.txtFormEndPt.Text = formEndPt.ToString();


            //if (this.ClientRectangle.Contains(cursorPos) == false)

            //should be >= then the max's for the hiding condition
            //i think there is a bug where when the position == the max pos
            //the leave event gets triggered
            if(cursorPos.X < formStartPt.X 
                || cursorPos.Y < formStartPt.Y
                || cursorPos.X >= formEndPt.X //TODO: should it be > or >=? 
                || cursorPos.Y >= formEndPt.Y )
            {
                this.hideStatus.Text = "Form should be hidden.";
                //this.WindowState = FormWindowState.Minimized;
                this.Hide();
                Thread.Sleep(new TimeSpan(0, 0, 5)); //wait 5 secs
                //this.WindowState = FormWindowState.Normal;
                this.Show();
                this.TopMost = true;
            }
            else
            {
                this.hideStatus.Text = "Form should NOT be hidden.";
            }
        }

        private void Form1_MouseEnter(object sender, EventArgs e)
        {
            //this.formStatus.Text = "Mouse Enter";
            //this.showForm();
        }

        private void Form1_MouseLeave(object sender, EventArgs e)
        {
            //this.formStatus.Text = "Mouse Leave";
            ////Point cursorPosWhenLeave = Cursor.
            //this.hideForm();
        }

        //does not seem to get triggered
        private void Form1_Enter(object sender, EventArgs e)
        {
            //this.formStatus.Text = "Enter";
        }

        //does not seem to get triggered
        private void Form1_Leave(object sender, EventArgs e)
        {
            //this.formStatus.Text = "Leave";
        }

        private void Form1_MouseMove(object sender, MouseEventArgs e)
        {
            //check if cursor is within bounds of form
            Point cursorPos = Cursor.Position;
            Point cursorPosClient = this.PointToClient(cursorPos);

            Console.WriteLine("cursorPos: " + cursorPos);
            this.txtCursorPos.Text = cursorPos.ToString();
            this.txtCursorPosClient.Text = cursorPosClient.ToString();

            //TODO: verify
            //this.hideForm();
        }

        private void panel1_MouseEnter(object sender, EventArgs e)
        {
            this.formStatus.Text = "Panel Mouse Enter";
            this.showForm();
        }

        private void panel1_MouseLeave(object sender, EventArgs e)
        {
            this.formStatus.Text = "Panel Mouse Leave";
            //Point cursorPosWhenLeave = Cursor.
            this.hideForm();
        }

        private void button1_Click_1(object sender, EventArgs e)
        {
            int xPos = 0;
            int yPos = 0;

            try
            {
                xPos = Int32.Parse(this.textBox1.Text);
            }
            catch (Exception ex)
            {
                Console.WriteLine(this.textBox1.Text + " is not a valid number");
            }

            try
            {
                yPos = Int32.Parse(this.textBox2.Text);

            }
            catch (Exception ex)
            {
                Console.WriteLine(this.textBox2.Text + " is not a valid number");
            }

            this.Location = new Point(xPos, yPos);
        }

        private void blockBtn_Click(object sender, EventArgs e)
        {
            TimeSpan span = new TimeSpan(0, 0, 5); //hours, minutes, seconds
            BlockInput(span);

            //this.Capture = true;
        }
       
    }
}
